﻿using System.Web;
using System.Web.Mvc;
using CML.Intercamber.Business;
using CML.Intercamber.Business.Dao;
using CML.Intercamber.Web.Helpers;

namespace CML.Intercamber.Web.Controllers
{
    [Authorize]
    //[InitializeSimpleMembership]
    public class ProfileController : BaseController
    {
        public ActionResult MyProfile()
        {
            var model = ConnectedUserHelper.ConnectedUser;
            var allCountries = CountryHelper.AllCountries;
            ViewBag.ContactCountries = allCountries;
            ViewBag.AllHobbies = HobbiesHelper.AllHobbies;
            ViewBag.MainInformationMissing = model.MainInformationMissing;

            return View("~/Views/Profile/MyProfile.cshtml", model);
        }


        [HttpPost]
        public ActionResult Edit(Users user, HttpPostedFileBase photo)
        {
            user.IdUser = ConnectedUserHelper.ConnectedUserId;

            if (photo != null && photo.ContentLength > 0)
            {
                // extract only the fieldname
                // TODO try catch 
                user.PhotoDirectory = ConnectedUserHelper.ConnectedUser.PhotoDirectory;
                Users.AddPhoto(user, photo, Server.MapPath("~/Images/_upload_"));
            }

            Users updatedUser = UsersDao.UpdateUser(user);
            if (updatedUser != null)
            {
                var updatedWholeBean = UsersDao.FindUserByEmail(updatedUser.Email);
                ConnectedUserHelper.CreateSession(updatedWholeBean);
            }

            return RedirectToAction("MyProfile");
        }
    }

}
